package com.klb.action.personal;

import com.klb.service.personal.impl.PersonalServicePersonalImpl;
import com.klb.tools.PostValue;
import com.klb.tools.Sessions;
import com.klb.tools.Tools;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fujia on 2016/6/8.
 */
public class MainActionPersonal extends ActionSupport {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private int articleId;
	
	private HashMap<String,Object> data;
	
    private PersonalServicePersonalImpl personalServicePersonal;

    

    public int getArticleId() {
		return articleId;
	}
	public void setArticleId(int articleId) {
		this.articleId = articleId;
	}


	public void setPersonalServicePersonal(PersonalServicePersonalImpl personalServicePersonal) {
		this.personalServicePersonal = personalServicePersonal;
	}

	@Override
    public String execute() throws Exception {

        if (!Tools.objectIsNullOrNot(Sessions.getSession("userId"))){
            int userId = (int) Sessions.getSession("userId");

            List<Map<String, Object>> myArticles = this.personalServicePersonal.getArticlesAndRepliesByUserId(userId);

            Sessions.setSession("myArticles", myArticles);

            return SUCCESS;
        }

        return ERROR;
    }

    public String postDelete(){

        if (this.personalServicePersonal.deleteArticle(this.articleId))
            return SUCCESS;

        return ERROR;
    }

    public String jsonForMain(){

        JSONObject jsonObject = PostValue.getJsonObject();

        if (!Tools.objectIsNullOrNot(jsonObject)){
            int userId = (int) jsonObject.get("userId");

            List<Map<String, Object>> myArticles = this.personalServicePersonal.getArticlesAndRepliesByUserId(userId);

            this.data = new HashMap<>();

            this.data.put("myArticles", myArticles);
            this.data.put("success", true);
        } else {
            this.data = null;
        }


        return SUCCESS;
    }


    public Map<String, Object> getData() {
        return data;
    }

}
